<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  // 使用类
  class InstanceCreate {
    constructor(name) {
      this.name = name
      if (!InstanceCreate.instance) InstanceCreate.instance = this
      return InstanceCreate.instance
    }
  }
  const i = new InstanceCreate('qwe')
  const j = new InstanceCreate('fasfsad')
  console.log(i === j);
  // 使用闭包
  function Singleton() {
    var Instance
    function createInstanceFn() {
      return '创建一个实例'
    }
    return function () {
      if (!Instance) Instance = createInstanceFn()
      return Instance
    }
  }

  const isc = Singleton()
  const promises = []
  function returnPromise() {
    return new Promise((resolve) => {
      promises.push((resolve1) => {
        test(resolve1)
      })
    })
  }
  returnPromise().then(res => {
    console.log(res);
  })
  returnPromise().then(res => {
    console.log(res);
  })
  console.log(promises);
  setTimeout(() => {
    promises.forEach(cb => cb())
  }, 2000);
  // const a = new Promise((resolve) => {
  //   test(resolve)
  // }).then(res => {
  //   console.log(res);
  // })
  function test(resolve) {
    // setTimeout(() => resolve(123), 1000)
    resolve(123)
  }
</script>

</html>